package com.example.blog.ws.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.UserMapper;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.User;
import com.example.blog.util.UserUtils.UserUtils;
import com.example.blog.ws.mapper.ChatMessageGroupMapper;
import com.example.blog.ws.mapper.ChatMessageSingleMapper;
import com.example.blog.ws.mapper.OnlineCountMapper;
import com.example.blog.ws.pojo.ChatMessageGroup;
import com.example.blog.ws.pojo.ChatMessageSingle;
import com.example.blog.ws.pojo.OnlineCount;
import com.example.blog.ws.service.ChatMassageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ChatMassageServiceImpl extends ServiceImpl<ChatMessageSingleMapper, ChatMessageSingle> implements ChatMassageService {

    @Resource
    private ChatMessageSingleMapper chatMessageSingleMapper;

    @Resource
    private UserMapper userMapper;
    @Resource
    private ChatMessageGroupMapper chatMessageGroupMapper;
    @Resource
    private OnlineCountMapper onlineCountMapper;
    @Resource
    private UserUtils userIdUtil;

    @Override
    public void saveGroupMessage(String fromUserId, String name, String headerUrl, String message, String messageType) {
        ChatMessageGroup chatMessageGroup = new ChatMessageGroup();
        chatMessageGroup.setFromUserId(fromUserId);
        chatMessageGroup.setMessage(message);
        chatMessageGroup.setName(name);
        chatMessageGroup.setHeaderUrl(headerUrl);
        chatMessageGroup.setDate(new Date());
        chatMessageGroup.setMessageType(messageType);
        chatMessageGroupMapper.insert(chatMessageGroup);
    }

    @Override
    public void saveSingleMessage(String fromUserId, String receiveUserId, String name, String headerUrl, String message, String messageType) {
        ChatMessageSingle chatMessageSingle = new ChatMessageSingle();
        chatMessageSingle.setFromUserId(fromUserId);
        chatMessageSingle.setReceiveUserId(receiveUserId);
        chatMessageSingle.setMessage(message);
        chatMessageSingle.setName(name);
        chatMessageSingle.setHeaderUrl(headerUrl);
        chatMessageSingle.setDate(new Date());
        chatMessageSingle.setMessageType(messageType);
        chatMessageSingleMapper.insert(chatMessageSingle);
    }

    @Override
    public Result<List<ChatMessageGroup>> getGroupMessage() {
        QueryWrapper<ChatMessageGroup> queryWrapper = new QueryWrapper<>();
        QueryWrapper<ChatMessageGroup> messageGroupQueryWrapper = queryWrapper
                .eq("message_type", "group")
                .orderByAsc("date"); // 按照时间字段升序排序

        List<ChatMessageGroup> chatMessageGroups = chatMessageGroupMapper.selectList(messageGroupQueryWrapper);
        return Result.success(chatMessageGroups);




    }

    @Override
    public Result<List<ChatMessageSingle>> getSingleMessage(String fromUserId, String receiveUserId) {
        QueryWrapper<ChatMessageSingle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("message_type", "single")
                .eq("from_user_id", fromUserId)
                .eq("receive_user_id", receiveUserId)
                .or()
                .eq("from_user_id", receiveUserId)
                .eq("receive_user_id", fromUserId);

        List<ChatMessageSingle> chatMessageSingles = chatMessageSingleMapper.selectList(queryWrapper);

        return Result.success(chatMessageSingles);
    }

    @Override
    public void saveOnlineCount(String count) {
        OnlineCount onlineCount=new OnlineCount();
        QueryWrapper<OnlineCount> queryWrapper=new QueryWrapper<>();
        QueryWrapper<OnlineCount> countQueryWrapper = queryWrapper.eq("id", 1);
        OnlineCount selectOne = onlineCountMapper.selectOne(countQueryWrapper);
        if (selectOne==null){
            onlineCount.setCount(count);
            onlineCount.setId(1);
            int insert = onlineCountMapper.insert(onlineCount);
            if (insert<=0){
                Result.failed("错误！");
                return;
            }
            Result.success(count);
            return;
        }
        onlineCount.setCount(count);
        onlineCount.setId(1);
        int update = onlineCountMapper.updateById(onlineCount);
        if (update>0){
            Result.success(count);
        }
    }

    @Override
    public Result<User> getUserByName(String name) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nickname",name);
        User user = userMapper.selectOne(queryWrapper);
        return Result.success(user);
    }

    @Override
    public Result<T> getOnlineCount() {
        QueryWrapper<OnlineCount> queryWrapper=new QueryWrapper<>();
        QueryWrapper<OnlineCount> count = queryWrapper.eq("id", 1);
        String onlineCount = onlineCountMapper.selectOne(count).getCount();
        return Result.success(onlineCount);
    }
}
